(module
  one
  (import finalize)
  (include "glfw.sch")
  (include "gl.sch")
  (include "glu.sch")
  (static (class s-quadric
             (glu-quadric::GLUquadric* (default (gluNewQuadric))))
          
          )
  (main main))

(define (s-quadric-finalizer obj)
   (gluDeleteQuadric (s-quadric-glu-quadric obj)))

(define (gen-s-quadric)
   (let
         ((val (instantiate::s-quadric)))
      (register-finalizer! s-quadric-finalizer val)
      val))

(define close #f)
(define
  (our-draw)
  (let* ((t::double (glfwGetTime))
         (field_of_view::double (+ 50.0 (* 30.0 (sin (* t 0.5)))))
         (quadric::s-quadric (gen-s-quadric))
         (camera_x::double (* 20.0 (cos (* 0.3 t))))
         (camera_z::double (* 20.0 (sin (* 0.3 t))))
         (camera_y::double (+ 4.0 (sin t))))
     
       (glViewport 0 0 640 480)
       (glClearColor 0.0 0.0 0.0 0.0)
       (glClear (bit-or GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
       
       (glMatrixMode GL_PROJECTION)
       (glLoadIdentity)
       (gluPerspective field_of_view (/ 640 480) 1.0 100.0)
       
       (glMatrixMode GL_MODELVIEW)
       (glLoadIdentity)
       (gluLookAt camera_x camera_y camera_z 0.0 0.0 0.0 0.0 1.0 0.0)
       (glEnable GL_DEPTH_TEST)
       (glDepthFunc GL_LEQUAL)

       (glColor3f 0.7 1.0 1.0)
       (glBegin GL_LINES)

       (do ((i::float -10.0 (+ 1 i)))
           ((> i 10.0) 1)
           (begin
              (glVertex3f -10.0 0.0 i) ;;line along x axis
              (glVertex3f 10.0 0.0 i)  ;;   -"-
              (glVertex3f i 0.0 -10.0)
              (glVertex3f i 0.0 10.0 )))
       (glEnd)

       
       ;; draw a blue cone in the center
       (glPushMatrix)
       (glRotatef -90.0 1.0 0.0 0.0)
       (glColor3f 0.0 0.0 1.0)
       (gluCylinder (s-quadric-glu-quadric quadric)
                    1.5 0.0 4.0 30 30)
       (glPopMatrix)

       ;; draw four spheres
       (glColor3f 1.0 0.2 0.0)
       (glPushMatrix)
       (glTranslatef -9.0 1.0 -9.0)
       (gluSphere (s-quadric-glu-quadric quadric) 1.0 30 30)
       (glPopMatrix)

       (glColor3f 1.0 0.2 0.0)
       (glPushMatrix)
       (glTranslatef 9.0 1.0 -9.0)
       (gluSphere (s-quadric-glu-quadric quadric) 1.0 30 30)
       (glPopMatrix)

       (glColor3f 1.0 0.2 0.0)
       (glPushMatrix)
       (glTranslatef -9.0 1.0 9.0)
       (gluSphere (s-quadric-glu-quadric quadric) 1.0 30 30)
       (glPopMatrix)

       (glColor3f 1.0 0.2 0.0)
       (glPushMatrix)
       (glTranslatef 9.0 1.0 9.0)
       (gluSphere (s-quadric-glu-quadric quadric) 1.0 30 30)
       (glPopMatrix)
       
       
       ))
       
      
(define
  (our-loop)
  (do ((count 0 (+ 1 count)))
      (close 0)
      (begin (our-draw)
             (glfwSwapBuffers)
             (when (or (= (glfwGetWindowParam GLFW_OPENED) 0)
                       (= GLFW_PRESS (glfwGetKey GLFW_KEY_ESC)))
                   (set! close #t)))))
(define
  (main argc)
  (glfwInit)
  (glfwOpenWindow 640 480 8 8 8 8 24 0 GLFW_WINDOW)
  (our-loop)
  (glfwTerminate))
